package com.cfh.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.cfh.ahead.Constant;
import com.cfh.domain.Config;
import com.cfh.domain.ConfigExample;
import com.cfh.domain.ConfigKey;
import com.cfh.domain.Product;
import com.cfh.domain.ProductExample;
import com.cfh.persistence.dao.ConfigDAO;
import com.cfh.persistence.dao.ProductDAO;

public class ConfigServiceImpl implements ConfigService {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(ConfigServiceImpl.class);

	/**
	 * Logger for this class
	 */

	private ConfigDAO configDAO;

	private ProductDAO productDAO;

	public void insert(Config Config) {
		configDAO.insert(Config);
	}

	public List selectByExample(ConfigExample example, int start, int limit) {
		return configDAO.selectByExample(example, start, limit);
	}

	public int countByExample(ConfigExample example) {
		return configDAO.countByExample(example);
	}

	public int update(Config Config) {
		return configDAO.updateByPrimaryKeySelective(Config);
	}

	public int deleteByExample(ConfigExample example) {
		return configDAO.deleteByExample(example);
	}

	public void setConfigDAO(ConfigDAO ConfigDAO) {
		this.configDAO = ConfigDAO;
	}

	public ConfigDAO getConfigDAO() {
		return configDAO;
	}

	// TODO REMOVE IT
	public Long getCurrentStage() {
		return new Long(0);

	}

	public int deleteByPrimaryKey(ConfigKey key) {
		return configDAO.deleteByPrimaryKey(key);
	}

	public List listFlv() {
		ProductExample example = new ProductExample();
		List list = productDAO.selectByExample(example);
		List langList = null;
		List toReturn = new ArrayList();
		int langCount = 0;
		{// get langs
			ConfigExample configExample = new ConfigExample();
			configExample.createCriteria().andCFG_TYPEEqualTo(Constant.CFG_TYPE_LANG);
			configExample.setOrderByClause(Constant.ORDERBY_LANG);
			langList = configDAO.selectByExample(configExample);
			langCount = langList.size();
		}

		for (Iterator iterator = list.iterator(); iterator.hasNext();) {
			Product item = (Product) iterator.next();
			for (Iterator iterator2 = langList.iterator(); iterator2.hasNext();) {
				Config config = (Config) iterator2.next();
				Map map = new HashMap();
				map.put(Constant.NAME, item.getPRD_NAME() + Constant.CHAR_CONNECT_2 + config.getCFG_VALUE());
				map.put(Constant.VALUE, item.getPRD_CODE() + Constant.CHAR_ + config.getCFG_CODE());
				toReturn.add(map);
			}
		}
		return toReturn;
	}

	public void setProductDAO(ProductDAO productDAO) {
		this.productDAO = productDAO;
	}

	public ProductDAO getProductDAO() {
		return productDAO;
	}

	public List listPdf() {
		ProductExample example = new ProductExample();
		List list = productDAO.selectByExample(example);
		List langList = null;
		List toReturn = new ArrayList();
		List companyList = null;
		int companyCount = 0;
		int langCount = 0;
		{// get langs
			ConfigExample configExample = new ConfigExample();
			configExample.createCriteria().andCFG_TYPEEqualTo(Constant.CFG_TYPE_LANG);
			configExample.setOrderByClause(Constant.ORDERBY_LANG);
			langList = configDAO.selectByExample(configExample);
			langCount = langList.size();
		}
		{// get companys
			ConfigExample configExample = new ConfigExample();
			configExample.createCriteria().andCFG_TYPEEqualTo(Constant.CFG_TYPE_COMPANY);
			configExample.setOrderByClause(Constant.ORDERBY_COMPANY);
			companyList = configDAO.selectByExample(configExample);
			companyCount = companyList.size();
		}
		for (Iterator iterator = list.iterator(); iterator.hasNext();) {
			Product item = (Product) iterator.next();
			for (Iterator iteratorCompany = companyList.iterator(); iteratorCompany.hasNext();) {
				Config configCompany = (Config) iteratorCompany.next();
				for (Iterator iteratorLang = langList.iterator(); iteratorLang.hasNext();) {
					Config configLang = (Config) iteratorLang.next();
					Map map = new HashMap();
					map.put(Constant.NAME, item.getPRD_NAME() + Constant.CHAR_CONNECT_2 + configCompany.getCFG_VALUE() + Constant.CHAR_CONNECT_2
							+ configLang.getCFG_VALUE());
					map.put(Constant.VALUE, item.getPRD_CODE() + Constant.CHAR_ + configCompany.getCFG_CODE() + Constant.CHAR_
							+ configLang.getCFG_CODE());
					toReturn.add(map);
				}
			}

		}
		return toReturn;
	}

	public void selectMoreFields(List list, String SERACHTYPE) {
		/*
		if (list == null) {
			return;
		}
		if (list.size() == 0) {
			return;
		}

		List langList = listLangs();
		List companyList = listCompanys();
		ProductExample example = new ProductExample();
		List prodList = productDAO.selectByExample(example);

		if (Constant.CFG_TYPE_PDF.equals(SERACHTYPE) || Constant.CFG_TYPE_BUYURL.equals(SERACHTYPE)
				|| Constant.CFG_TYPE_INTRO.equals(SERACHTYPE)) {
			for (Iterator iterator = list.iterator(); iterator.hasNext();) {
				Config listItem = (Config) iterator.next();
				moreFieldsType1(listItem,prodList,langList,companyList);
			}
		}else if(Constant.CFG_TYPE_FLV.equals(SERACHTYPE)){
			for (Iterator iterator = list.iterator(); iterator.hasNext();) {
				Config listItem = (Config) iterator.next();
				moreFieldsType2(listItem,prodList,langList);
			}
			
		}
		*/

	}
	
	private void moreFieldsType2(Config listItem, List prodList, List langList) {
		if (listItem == null) {
			return;
		}
		String id = listItem.getCFG_CODE();
		if(id == null || id.length() == 0){
			return;
		}
		String[] ids = id.split(Constant.CHAR_);
		if(ids == null || ids.length!=2){
			return;
		}
		String prdId = ids[0];
		String langId = ids[1];
		
		for (Iterator iterator = prodList.iterator(); iterator.hasNext();) {
			Product prd = (Product) iterator.next();
			if(prd.getPRD_CODE().equals(prdId)){
				listItem.setMM_PRODUCT(prd.getPRD_NAME());
			}
		}
		
		for (Iterator iterator = langList.iterator(); iterator.hasNext();) {
			Config tmpConfig = (Config) iterator.next();
			if(tmpConfig.getCFG_CODE().equals(langId)){
				listItem.setMM_LANG(tmpConfig.getCFG_VALUE());
			}
		}
		
	}

	private void moreFieldsType1(Config listItem, List prodList, List langList, List companyList) {
		if (listItem == null) {
			return;
		}
		String id = listItem.getCFG_CODE();
		if(id == null || id.length() == 0){
			return;
		}
		String[] ids = id.split(Constant.CHAR_);
		if(ids == null || ids.length!=3){
			return;
		}
		String prdId = ids[0];
		String companyId = ids[1];
		String langId = ids[2];
		
		for (Iterator iterator = prodList.iterator(); iterator.hasNext();) {
			Product prd = (Product) iterator.next();
			if(prd.getPRD_CODE().equals(prdId)){
				listItem.setMM_PRODUCT(prd.getPRD_NAME());
			}
		}
		
		for (Iterator iterator = langList.iterator(); iterator.hasNext();) {
			Config tmpConfig = (Config) iterator.next();
			if(tmpConfig.getCFG_CODE().equals(langId)){
				listItem.setMM_LANG(tmpConfig.getCFG_VALUE());
			}
		}
		
		for (Iterator iterator = companyList.iterator(); iterator.hasNext();) {
			Config tmpConfig = (Config) iterator.next();
			if(tmpConfig.getCFG_CODE().equals(companyId)){
				listItem.setMM_COMPANY(tmpConfig.getCFG_VALUE());
			}
		}
	}

	public List listLangs(){
		ConfigExample configExample = new ConfigExample();
		configExample.createCriteria().andCFG_TYPEEqualTo(Constant.CFG_TYPE_LANG);
		configExample.setOrderByClause(Constant.ORDERBY_LANG);
		return configDAO.selectByExample(configExample);
	}
	
	public List listCompanys(){
		ConfigExample configExample = new ConfigExample();
		configExample.createCriteria().andCFG_TYPEEqualTo(Constant.CFG_TYPE_COMPANY);
		configExample.setOrderByClause(Constant.ORDERBY_COMPANY);
		return configDAO.selectByExample(configExample);
	}
	
	public static void main(String[] args){
		
		String id = "AIO_L1_C1";
		if(id == null || id.length() == 0){
			return;
		}
		String[] ids = id.split(Constant.CHAR_);
		for (int i = 0; i < ids.length; i++) {
			System.out.println(i+":"+ids[i]);
		}
		
	}

}
